{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.8 \u5b50\u7c7b\u4e2d\u6269\u5c55property\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5b50\u7c7b\u4e2d\uff0c\u4f60\u60f3\u8981\u6269\u5c55\u5b9a\u4e49\u5728\u7236\u7c7b\u4e2d\u7684property\u7684\u529f\u80fd\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8003\u8651\u5982\u4e0b\u7684\u4ee3\u7801\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2aproperty\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Person:\n    def __init__(self, name):\n        self.name = name\n\n    # Getter function\n    @property\n    def name(self):\n        return self._name\n\n    # Setter function\n    @name.setter\n    def name(self, value):\n        if not isinstance(value, str):\n            raise TypeError('Expected a string')\n        self._name = value\n\n    # Deleter function\n    @name.deleter\n    def name(self):\n        raise AttributeError(\"Can't delete attribute\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7c7b\uff0c\u5b83\u7ee7\u627f\u81eaPerson\u5e76\u6269\u5c55\u4e86 name \u5c5e\u6027\u7684\u529f\u80fd\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class SubPerson(Person):\n    @property\n    def name(self):\n        print('Getting name')\n        return super().name\n\n    @name.setter\n    def name(self, value):\n        print('Setting name to', value)\n        super(SubPerson, SubPerson).name.__set__(self, value)\n\n    @name.deleter\n    def name(self):\n        print('Deleting name')\n        super(SubPerson, SubPerson).name.__delete__(self)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u63a5\u4e0b\u6765\u4f7f\u7528\u8fd9\u4e2a\u65b0\u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = SubPerson('Guido')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.name"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.name = 'Larry'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.name = 42"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4ec5\u4ec5\u53ea\u60f3\u6269\u5c55property\u7684\u67d0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u90a3\u4e48\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class SubPerson(Person):\n    @Person.name.getter\n    def name(self):\n        print('Getting name')\n        return super().name"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6216\u8005\uff0c\u4f60\u53ea\u60f3\u4fee\u6539setter\u65b9\u6cd5\uff0c\u5c31\u8fd9\u4e48\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class SubPerson(Person):\n    @Person.name.setter\n    def name(self, value):\n        print('Setting name to', value)\n        super(SubPerson, SubPerson).name.__set__(self, value)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5b50\u7c7b\u4e2d\u6269\u5c55\u4e00\u4e2aproperty\u53ef\u80fd\u4f1a\u5f15\u8d77\u5f88\u591a\u4e0d\u6613\u5bdf\u89c9\u7684\u95ee\u9898\uff0c\n\u56e0\u4e3a\u4e00\u4e2aproperty\u5176\u5b9e\u662f getter\u3001setter \u548c deleter \u65b9\u6cd5\u7684\u96c6\u5408\uff0c\u800c\u4e0d\u662f\u5355\u4e2a\u65b9\u6cd5\u3002\n\u56e0\u6b64\uff0c\u5f53\u4f60\u6269\u5c55\u4e00\u4e2aproperty\u7684\u65f6\u5019\uff0c\u4f60\u9700\u8981\u5148\u786e\u5b9a\u4f60\u662f\u5426\u8981\u91cd\u65b0\u5b9a\u4e49\u6240\u6709\u7684\u65b9\u6cd5\u8fd8\u662f\u8bf4\u53ea\u4fee\u6539\u5176\u4e2d\u67d0\u4e00\u4e2a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u7684property\u65b9\u6cd5\u90fd\u88ab\u91cd\u65b0\u5b9a\u4e49\u3002\n\u5728\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u4e86 super() \u6765\u8c03\u7528\u7236\u7c7b\u7684\u5b9e\u73b0\u3002\n\u5728 setter \u51fd\u6570\u4e2d\u4f7f\u7528 super(SubPerson, SubPerson).name.__set__(self, value) \u7684\u8bed\u53e5\u662f\u6ca1\u6709\u9519\u7684\u3002\n\u4e3a\u4e86\u59d4\u6258\u7ed9\u4e4b\u524d\u5b9a\u4e49\u7684setter\u65b9\u6cd5\uff0c\u9700\u8981\u5c06\u63a7\u5236\u6743\u4f20\u9012\u7ed9\u4e4b\u524d\u5b9a\u4e49\u7684name\u5c5e\u6027\u7684 __set__() \u65b9\u6cd5\u3002\n\u4e0d\u8fc7\uff0c\u83b7\u53d6\u8fd9\u4e2a\u65b9\u6cd5\u7684\u552f\u4e00\u9014\u5f84\u662f\u4f7f\u7528\u7c7b\u53d8\u91cf\u800c\u4e0d\u662f\u5b9e\u4f8b\u53d8\u91cf\u6765\u8bbf\u95ee\u5b83\u3002\n\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u6211\u4eec\u8981\u4f7f\u7528 super(SubPerson, SubPerson) \u7684\u539f\u56e0\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u53ea\u60f3\u91cd\u5b9a\u4e49\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\uff0c\u90a3\u53ea\u4f7f\u7528 @property \u672c\u8eab\u662f\u4e0d\u591f\u7684\u3002\u6bd4\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u65e0\u6cd5\u5de5\u4f5c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class SubPerson(Person):\n    @property  # Doesn't work\n    def name(self):\n        print('Getting name')\n        return super().name"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8bd5\u7740\u8fd0\u884c\u4f1a\u53d1\u73b0setter\u51fd\u6570\u6574\u4e2a\u6d88\u5931\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = SubPerson('Guido')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5e94\u8be5\u50cf\u4e4b\u524d\u8bf4\u8fc7\u7684\u90a3\u6837\u4fee\u6539\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class SubPerson(Person):\n    @Person.name.getter\n    def name(self):\n        print('Getting name')\n        return super().name"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e48\u5199\u540e\uff0cproperty\u4e4b\u524d\u5df2\u7ecf\u5b9a\u4e49\u8fc7\u7684\u65b9\u6cd5\u4f1a\u88ab\u590d\u5236\u8fc7\u6765\uff0c\u800cgetter\u51fd\u6570\u88ab\u66ff\u6362\u3002\u7136\u540e\u5b83\u5c31\u80fd\u6309\u7167\u671f\u671b\u7684\u5de5\u4f5c\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = SubPerson('Guido')\ns.name"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.name = 'Larry'\ns.name"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.name = 42"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u7279\u522b\u7684\u89e3\u51b3\u65b9\u6848\u4e2d\uff0c\u6211\u4eec\u6ca1\u529e\u6cd5\u4f7f\u7528\u66f4\u52a0\u901a\u7528\u7684\u65b9\u5f0f\u53bb\u66ff\u6362\u786c\u7f16\u7801\u7684 Person \u7c7b\u540d\u3002\n\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5230\u5e95\u662f\u54ea\u4e2a\u57fa\u7c7b\u5b9a\u4e49\u4e86property\uff0c\n\u90a3\u4f60\u53ea\u80fd\u901a\u8fc7\u91cd\u65b0\u5b9a\u4e49\u6240\u6709property\u5e76\u4f7f\u7528 super() \u6765\u5c06\u63a7\u5236\u6743\u4f20\u9012\u7ed9\u524d\u9762\u7684\u5b9e\u73b0\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u503c\u5f97\u6ce8\u610f\u7684\u662f\u4e0a\u9762\u6f14\u793a\u7684\u7b2c\u4e00\u79cd\u6280\u672f\u8fd8\u53ef\u4ee5\u88ab\u7528\u6765\u6269\u5c55\u4e00\u4e2a\u63cf\u8ff0\u5668(\u57288.9\u5c0f\u8282\u6211\u4eec\u6709\u4e13\u95e8\u7684\u4ecb\u7ecd)\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# A descriptor\nclass String:\n    def __init__(self, name):\n        self.name = name\n\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        return instance.__dict__[self.name]\n\n    def __set__(self, instance, value):\n        if not isinstance(value, str):\n            raise TypeError('Expected a string')\n        instance.__dict__[self.name] = value\n\n# A class with a descriptor\nclass Person:\n    name = String('name')\n\n    def __init__(self, name):\n        self.name = name\n\n# Extending a descriptor with a property\nclass SubPerson(Person):\n    @property\n    def name(self):\n        print('Getting name')\n        return super().name\n\n    @name.setter\n    def name(self, value):\n        print('Setting name to', value)\n        super(SubPerson, SubPerson).name.__set__(self, value)\n\n    @name.deleter\n    def name(self):\n        print('Deleting name')\n        super(SubPerson, SubPerson).name.__delete__(self)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8bfb\u5230\u8fd9\u91cc\u65f6\uff0c\u4f60\u5e94\u8be5\u4f1a\u53d1\u73b0\u5b50\u7c7b\u5316 setter \u548c deleter \u65b9\u6cd5\u5176\u5b9e\u662f\u5f88\u7b80\u5355\u7684\u3002\n\u8fd9\u91cc\u6f14\u793a\u7684\u89e3\u51b3\u65b9\u6848\u540c\u6837\u9002\u7528\uff0c\u4f46\u662f\u5728 Python\u7684issue\u9875\u9762\n\u62a5\u544a\u7684\u4e00\u4e2abug\uff0c\u6216\u8bb8\u4f1a\u4f7f\u5f97\u5c06\u6765\u7684Python\u7248\u672c\u4e2d\u51fa\u73b0\u4e00\u4e2a\u66f4\u52a0\u7b80\u6d01\u7684\u65b9\u6cd5\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}